package com.qqletter.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author Devin.Hu
 * @date 2011-8-13
 * @version V1.0
 * @description 字符处理工具类
 */
public abstract class StringUtils {

	private StringUtils() {}

	/**
	 * 是否为空
	 * 
	 * @param value
	 * @return
	 */
	public static boolean isEmpty(String value) {
		int strLen;
		if (value == null || (strLen = value.length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if ((Character.isWhitespace(value.charAt(i)) == false)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 是否为数字
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isNumeric(Object obj) {
		if (obj == null) {
			return false;
		}
		String str = obj.toString();
		int sz = str.length();
		for (int i = 0; i < sz; i++) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}


	/**
	 * 数组是否为空
	 * 
	 * @param values
	 * @return
	 */
	public static boolean areNotEmpty(String... values) {
		boolean result = true;
		if (values == null || values.length == 0) {
			result = false;
		} else {
			for (String value : values) {
				result &= !isEmpty(value);
			}
		}
		return result;
	}

	/**
	 * @param unicode
	 * @return
	 */
	public static String unicodeToChinese(String unicode) {
		StringBuilder out = new StringBuilder();
		if (!isEmpty(unicode)) {
			for (int i = 0; i < unicode.length(); i++) {
				out.append(unicode.charAt(i));
			}
		}
		return out.toString();
	}
	
	/**
	 * @param input
	 * @return
	 */
	public static String stripNonValidXMLCharacters(String input) {
		if (input == null || ("".equals(input)))
			return "";
		StringBuilder out = new StringBuilder();
		char current;
		for (int i = 0; i < input.length(); i++) {
			current = input.charAt(i);
			if ((current == 0x9) || (current == 0xA) || (current == 0xD)
					|| ((current >= 0x20) && (current <= 0xD7FF))
					|| ((current >= 0xE000) && (current <= 0xFFFD))
					|| ((current >= 0x10000) && (current <= 0x10FFFF)))
				out.append(current);
		}
		return out.toString();
	}
	
    /**
     * 字符串按字节截取 
     * 
     * @param str 原字符 
     * @param len 截取长度
     * @return
     */
    public static String splitString(String str, int len) {  
    	return splitString(str, len, "...");  
    }  
    
	 /** 
     * 字符串按字节截取 
     * 
     * @param str 原字符 
     * @param len 截取长度 
     * @param elide 省略符 
     * @return String 
     */  
     public static String splitString(String str,int len,String elide) {  
        if (str == null) return "";  
        int strlen = str.length();
        if(strlen - len > 0 ){
            str = str.substring(0, len) + elide.trim();
        }
        return str;
     }  
     
    /**
     * 清除掉所有特殊字符
     * @param str
     * @return
     */
    public static String StringFilter(String str){
    	if(str==null || str.equals(""))return str;
    	String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";  
    	Pattern p = Pattern.compile(regEx);     
    	Matcher m = p.matcher(str);     
    	return m.replaceAll("").trim();     
    } 
    
    /**
     * 根据1 、 0 转化成是 、否
     * @param str
     * @return
     */
    public static String isOrNo2String(String str){
    	if(null != str){
    		if(str.equals("1")) return "是";
    	}
    	return "否";
    }
    
    /**
     * 将分转化成元
     * @param price
     * @return
     */
    public static String getFormatPrice(String price){
    	if(null != price){
    		double mprice = Double.parseDouble(price)/100;
    		return String.valueOf(mprice);
    	}
    	return "0.0";
    }
    
    /**
	 * 得到当天
	 * @return
	 */
	public static String getCurDate()
	{  
		return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
	}
	
	/**
	 * 处理拍拍中的价格，因为其默认以分为单位
	 * @param price
	 * @return
	 */
	public static String getPrice(String price){
		double temp = 0.0;
		if(!StringUtils.isEmpty(price)){
			temp = Double.parseDouble(price)/100;
		}
		return String.valueOf(temp);
	}
}
